package ru.volgogradetzzz.display {
	import flash.display.DisplayObject;
	import ru.volgogradetzzz.math.VMatrix2D;
	
	/**
	 * ...
	 * @author Volgogradetzzz
	 */
	public class DisplayObjectContainer2D extends DisplayObject2D {
		
		public var firstChild:DisplayObject2D;
		public var lastChild:DisplayObject2D;
		
		public function addChild(child:DisplayObject2D):void {
			if (firstChild == null) {
				firstChild = child;
			}else {
				lastChild.next = child;
				child.prev = lastChild;
			}
			
			if (stage) {
				child.stage = stage;
			}
			
			child.parent = this;
			child.transform();
			
			lastChild = child;
			
			totalDescendants++;
			
			var num:uint = child.totalDescendants;
			totalDescendants += num;
			
			num++;
			
			var par:DisplayObjectContainer2D = parent;
			while (par) {
				par.totalDescendants += num;
				par = par.parent;
			}
			
			if (stage) {
				stage.totalDescendants += num;
				
				child.updateIndex();
				child.addData();
			}
		}
		
		public function removeChild(child:DisplayObject2D):void {
			
		}
		
		public function removeChildren():void {
			
		}
		
		override public function transform():void
		{
			super.transform();
			
			var child:DisplayObject2D = firstChild;
			while (child != null) {
				child.transform();
				child = child.next;
			}
		}
		
		override public function updateIndex():void
		{
			super.updateIndex();
			
			var child:DisplayObject2D = firstChild;
			while (child != null) {
				child.updateIndex();
				child = child.next;
			}
		}
		
		override public function updateData():void {
			super.updateData();
			
			var child:DisplayObject2D = firstChild;
			while (child != null) {
				child.updateData();
				child = child.next;
			}
		}
		
		public override function set stage(s:Stage2D):void
		{
			super.stage = s;
			
			var child:DisplayObject2D = firstChild;
			while (child != null) {
				child.stage = s;
				child = child.next;
			}
		}
	}
}